package com.example.emergencyattempt01.activity.ex;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public  class AutoPager  {

    List<Bitmap> pagerBitmaps = new ArrayList<>();//图片资源表
    ImageView[] dots = new ImageView[5];// 小圆点资源表
    ViewPager viewPager;

    private final int MOVE_TO_NEXT = 0x001;
     Handler handlerForAutoAdapter;// 用于实现自身的自动播放的handler

    public class Autoplayer extends Thread // 自动播放线程,属于autopager自身功能，不予预设，自己启动就ok
    {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int i = (viewPager.getCurrentItem() + 1) % 5;
                Message message = new Message();
                message.what = MOVE_TO_NEXT;
                message.obj = i;
                handlerForAutoAdapter.sendMessage(message);
            }
        }
    }


    // 设置图片表
    public void setPagerBitmaps(List<Bitmap> pagerBitmaps) {
        this.pagerBitmaps = pagerBitmaps;
    }

    // 设置小圆点数组
    public void setDots(ImageView[] dots) {
        this.dots = dots;
    }

    public void setHandlerForAutoAdapter(Handler handlerForAutoAdapter) {
        this.handlerForAutoAdapter = handlerForAutoAdapter;
    }
    // 设置播放线程
    public void start()
    {
        new Autoplayer().start();
    }
}